/*
 * CorpusDestinationPanel.java
 *
 */

package drayson.weboca.gui;

import java.awt.Component;
import javax.swing.JFileChooser;
import javax.swing.JRadioButton;
import org.netbeans.spi.wizard.WizardPage;

/**
 * A GUI that allows the user to save the corpus to a chosen destination, and choose the format of the to-be saved corpus
 * 
 * @author  Michael Drayson
 * @version 1.0
 */
public class CorpusDestinationPanel extends WizardPage {
    
    private JFileChooser fc;
    
    /** Creates new form CorpusDestinationPanel */
    public CorpusDestinationPanel() {
        super("Corpus Destination", "Corpus Destination");
        setupFileChooserDialog();

                
        initComponents();
        
        rdoFormatRaw.putClientProperty ("corpusFormat", "raw");
        rdoFormatVertical.putClientProperty ("corpusFormat", "vertical");
        
        // Set default (which is raw)
        putWizardData("corpusFormat", "vertical");
        System.out.println((String)getWizardData("corpusFormat"));
        
  
        
    }
    
    // The following code stops the page from continuing until a condition is met!
    protected String validateContents (Component component, Object o) {
    
        if (txtFilename.getText().trim().equals("")) {
            return "Specify a location to save the corpus.";
        }
        // The below code ensures that the location can be written to, however currently doesn't work
        /*else if (!new File(txtFilename.getText().trim()).canWrite()) {
                return "Can't save to this location.";
        }*/
        return null;
    }
    
    public static String getDescription() {
        
        return "Corpus Destination";
    }
    
    
    
    private void setupFileChooserDialog() {
        
        
            fc = new JFileChooser();
        
    }
    
    private void formatSelect(java.awt.event.ActionEvent evt) {
        JRadioButton button = (JRadioButton) evt.getSource();
        putWizardData("corpusFormat", button.getClientProperty("corpusFormat"));
               
    }
    

  
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btngrCorpusFormat = new javax.swing.ButtonGroup();
        lblFilename = new javax.swing.JLabel();
        txtFilename = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();
        lblInstruction = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        rdoFormatRaw = new javax.swing.JRadioButton();
        corpusLabel = new javax.swing.JLabel();
        rdoFormatVertical = new javax.swing.JRadioButton();
        noteLabel = new javax.swing.JLabel();

        setEnabled(false);
        lblFilename.setText("Filename");

        txtFilename.setName("txtFilename");

        btnBrowse.setText("Browse...");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        lblInstruction.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblInstruction.setText("Select a location to save the new corpus");

        jLabel1.setText("Encoding");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UTF8" }));
        jComboBox1.setEnabled(false);

        btngrCorpusFormat.add(rdoFormatRaw);
        rdoFormatRaw.setText("Horizontal");
        rdoFormatRaw.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rdoFormatRaw.setEnabled(false);
        rdoFormatRaw.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rdoFormatRaw.setName("rdoFormatRaw");
        rdoFormatRaw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoFormatRawActionPerformed(evt);
            }
        });

        corpusLabel.setText("Corpus format");
        corpusLabel.setEnabled(false);

        btngrCorpusFormat.add(rdoFormatVertical);
        rdoFormatVertical.setSelected(true);
        rdoFormatVertical.setText("Vertical");
        rdoFormatVertical.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rdoFormatVertical.setEnabled(false);
        rdoFormatVertical.setMargin(new java.awt.Insets(0, 0, 0, 0));
        rdoFormatVertical.setName("rdoFormatVertical");
        rdoFormatVertical.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdoFormatVerticalActionPerformed(evt);
            }
        });

        noteLabel.setFont(new java.awt.Font("Tahoma", 2, 11));
        noteLabel.setText("(Please note you cannot process a Horizontal corpus)");
        noteLabel.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblInstruction)
                    .add(layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblFilename)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(txtFilename, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 223, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(20, 20, 20)
                                .add(btnBrowse))
                            .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(corpusLabel)
                    .add(layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(rdoFormatRaw)
                            .add(rdoFormatVertical)
                            .add(layout.createSequentialGroup()
                                .add(17, 17, 17)
                                .add(noteLabel)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(lblInstruction)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblFilename)
                    .add(btnBrowse)
                    .add(txtFilename, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(19, 19, 19)
                .add(corpusLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rdoFormatVertical)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rdoFormatRaw)
                .add(14, 14, 14)
                .add(noteLabel)
                .addContainerGap(117, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void rdoFormatVerticalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoFormatVerticalActionPerformed
        formatSelect(evt);
    }//GEN-LAST:event_rdoFormatVerticalActionPerformed

    private void rdoFormatRawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdoFormatRawActionPerformed
        formatSelect(evt);
    }//GEN-LAST:event_rdoFormatRawActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        int returnVal = fc.showSaveDialog(this);

	if (returnVal == JFileChooser.APPROVE_OPTION) {
            String oldText = txtFilename.getText();
            txtFilename.setText(fc.getSelectedFile().getAbsolutePath());
            
        }
        
        corpusLabel.setEnabled(true);
        rdoFormatVertical.setEnabled(true);
        
       if ((String)getWizardData("userType") == "simple")
       {
            rdoFormatRaw.setEnabled(true);
       }
        
       lblInstruction.setFont(new java.awt.Font("Tahoma", 0, 11));
       corpusLabel.setEnabled(true);
       corpusLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
       rdoFormatVertical.setEnabled(true);
       noteLabel.setEnabled(true);
       
       
      
    }//GEN-LAST:event_btnBrowseActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.ButtonGroup btngrCorpusFormat;
    private javax.swing.JLabel corpusLabel;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblFilename;
    private javax.swing.JLabel lblInstruction;
    private javax.swing.JLabel noteLabel;
    public static javax.swing.JRadioButton rdoFormatRaw;
    public static javax.swing.JRadioButton rdoFormatVertical;
    private javax.swing.JTextField txtFilename;
    // End of variables declaration//GEN-END:variables
    
}
